package cn.ctodb.openapi.idcard.service.impl;

import cn.ctodb.openapi.idcard.service.DataIdcardService;
import cn.ctodb.openapi.idcard.domain.DataIdcard;
import cn.ctodb.openapi.idcard.repository.DataIdcardRepository;
import cn.ctodb.openapi.idcard.service.dto.DataIdcardDTO;
import cn.ctodb.openapi.idcard.service.mapper.DataIdcardMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Service Implementation for managing DataIdcard.
 */
@Service
@Transactional
public class DataIdcardServiceImpl implements DataIdcardService {

    private final Logger log = LoggerFactory.getLogger(DataIdcardServiceImpl.class);

    private final DataIdcardRepository dataIdcardRepository;

    private final DataIdcardMapper dataIdcardMapper;

    public DataIdcardServiceImpl(DataIdcardRepository dataIdcardRepository, DataIdcardMapper dataIdcardMapper) {
        this.dataIdcardRepository = dataIdcardRepository;
        this.dataIdcardMapper = dataIdcardMapper;
    }

    /**
     * Save a dataIdcard.
     *
     * @param dataIdcardDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public DataIdcardDTO save(DataIdcardDTO dataIdcardDTO) {
        log.debug("Request to save DataIdcard : {}", dataIdcardDTO);
        DataIdcard dataIdcard = dataIdcardMapper.dataIdcardDTOToDataIdcard(dataIdcardDTO);
        dataIdcard = dataIdcardRepository.save(dataIdcard);
        DataIdcardDTO result = dataIdcardMapper.dataIdcardToDataIdcardDTO(dataIdcard);
        return result;
    }

    /**
     * Get all the dataIdcards.
     *
     * @param pageable the pagination information
     * @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public Page<DataIdcardDTO> findAll(Pageable pageable) {
        log.debug("Request to get all DataIdcards");
        Page<DataIdcard> result = dataIdcardRepository.findAll(pageable);
        return result.map(dataIdcard -> dataIdcardMapper.dataIdcardToDataIdcardDTO(dataIdcard));
    }

    /**
     * Get one dataIdcard by id.
     *
     * @param id the id of the entity
     * @return the entity
     */
    @Override
    @Transactional(readOnly = true)
    public DataIdcardDTO findOne(Long id) {
        log.debug("Request to get DataIdcard : {}", id);
        DataIdcard dataIdcard = dataIdcardRepository.findOne(id);
        DataIdcardDTO dataIdcardDTO = dataIdcardMapper.dataIdcardToDataIdcardDTO(dataIdcard);
        return dataIdcardDTO;
    }

    /**
     * Delete the  dataIdcard by id.
     *
     * @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete DataIdcard : {}", id);
        dataIdcardRepository.delete(id);
    }

    @Override
    public DataIdcardDTO findOneByNameAndCardno(String name, String cardno) {
        DataIdcard dataIdcard = dataIdcardRepository.findOneByNameAndCardno(name, cardno);
        DataIdcardDTO dataIdcardDTO = dataIdcardMapper.dataIdcardToDataIdcardDTO(dataIdcard);
        return dataIdcardDTO;
    }
}
